#coding:utf8
#
#设备变更信息维护
#
import cherrypy
import datetime
import os
from ConnectMSSQL import ms

from head import *
from menu import *
from end import *

from utf8code import *
from xxtj import *

_js = """
        <script type="text/javascript">
            var bgdh;
            $(function()
            {                
                //高亮显示
                $(".table tr").hover
                (
                    function() { $(this).addClass("highlight"); },
                    function() { $(this).removeClass("highlight"); }
                );
                
                //单选框默认选中.变色.
                $('.table input[type="radio"]:checked').parents('tr').addClass('selected');
                
                //单击
                $('.table tr').click(function()
                {
                    $(this).siblings().removeClass('selected');
                    $(this).addClass('selected');
                    $(this).find('input[type="radio"]').attr('checked', 'checked');
                    bgdh=$(this).find('td[id="1"]').text();
                });
                
                $("#delbut").click(function()
                {
                    if(bgdh)
                    {
                        var ret=confirm("确定删除编号为:" + bgdh + "的变更记录?此操作不可恢复!请谨慎操作!")
                        if(ret == false)
                            return;
                        else
                            //ajax为删除结果做显示
                            var xmlhttp;
                            if (window.XMLHttpRequest)
                            {// code for IE7+, Firefox, Chrome, Opera, Safari
                              xmlhttp=new XMLHttpRequest();
                            }
                            else
                            {// code for IE6, IE5
                              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                            }
                        
                            xmlhttp.onreadystatechange=function()
                            {
                              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                                {
                                    if(xmlhttp.responseText == "no")
                                    {
                                        alert("删除失败!-_-|| 请从最近一次变更往前删!");
                                    }
                                    else
                                    {
                                        alert("删除成功!*^_^*");
                                        location.reload();
                                    }
                                }
                            }
                            xmlhttp.open("GET","/sbbgxx/delBG?bgdh="+bgdh,true);
                            xmlhttp.send();
                    }
                    else
                        alert("请选择一台设备");
                });
            });
        </script>
    """

_js_noadmin = """
        <script type="text/javascript">
            $(function()
            {
                $("#delbut").hide();
            });
        </script>
"""

_top = """
    <form method="get" action="/sbbgxx/serchBG">
        <div id="div_operate">
            <input type="button" id="delbut" value="删除" />
                                查询条件：
            <input type="text" id="serchtext" name="cxtj" onfocus="hidevalue()" placeholder="变更人、设备编号" />
            <input type="submit" id="serchbut" value="查询" />
            <input type="button" id="exitbut" onclick="exit()" value="关闭" />
        </div>
        <div id="div_data">
            <table width="auto" class="table">
              <tr class="class_tr_th">
                <th class="class_th" scope="col">√</th>
                <th class="class_th" scope="col">#</th>
                <th class="class_th" scope="col">变更单号</th>
                <th class="class_th" scope="col">变更日期↓</th>
                <th class="class_th" scope="col">部门</th>
                <th class="class_th" scope="col">变更人</th>
                <th class="class_th" scope="col">设备编号</th>
                <th class="class_th" scope="col">经手人</th>
                <th class="class_th" scope="col">备注</th>
              </tr>
"""

_end = """
            </table>
        </div>
    </form>
"""
class BGXX:
    _mid_tmp = """            
            <tr class="class_tr">
                <td><input type="radio" id="select" name="selradio" /></td>
                <td>%s</td>
                <td id="%s">%s</td>
                <td>%s</td>
                <td>%s</td>
                <td>%s</td>
                <td>%s</td>
                <td>%s</td>
                <td>%s</td>
            </tr>
    """
    @cherrypy.expose
    def index(self):
        sql_bgxx = """
            SELECT A.BGDH
                ,A.BGRQ
                ,B.BM
                ,B.RY
                ,A.SBBH
                ,A.JSR
                ,A.BZ
            FROM T_SBBG A
            LEFT JOIN T_BMRY B ON A.RYID = B.ID
            ORDER BY A.BGRQ DESC;
        """        
        dataList = ms.ExecQuery(sql_bgxx)
        _mid_2 = ''
        i = 0
        for bgxx in dataList:
            i += 1
            _mid_2 += self._mid_tmp % (i,1,bgxx[0],datetime.datetime.strftime(bgxx[1],'%Y-%m-%d'),bgxx[2],bgxx[3],bgxx[4],bgxx[5],bgxx[6])
        user = cherrypy.session.get('user')
        if user != 'admin':
            js = _js + _js_noadmin
        else:
            js = _js
        return utf8code(print_head % ('设备变更记录信息','设备变更记录信息') + js + _top + _mid_2 + _end + div_xxtj % (cherrypy.session.get('user'),len(dataList)) + print_end)
    
    @cherrypy.expose
    def serchBG(self,cxtj,selradio=None):
        sql_bgxx = """
            SELECT A.BGDH
                ,A.BGRQ
                ,B.BM
                ,B.RY
                ,A.SBBH
                ,A.JSR
                ,A.BZ
            FROM T_SBBG A
            LEFT JOIN T_BMRY B ON A.RYID = B.ID
            WHERE B.RY LIKE '%""" + cxtj + "%' OR " \
            + "A.SBBH LIKE '%" + cxtj + "%' " \
            + 'ORDER BY A.BGRQ DESC'
        dataList = ms.ExecQuery(sql_bgxx)
        _mid_2 = ''
        i = 0
        for bgxx in dataList:
            i += 1
            _mid_2 += self._mid_tmp % (i,1,bgxx[0],datetime.datetime.strftime(bgxx[1],'%Y-%m-%d'),bgxx[2],bgxx[3],bgxx[4],bgxx[5],bgxx[6])
        user = cherrypy.session.get('user')
        if user != 'admin':
            js = _js + _js_noadmin
        else:
            js = _js
        return utf8code(print_head % ('设备变更记录信息','设备变更记录信息') + js + _top + _mid_2 + _end + div_xxtj % (cherrypy.session.get('user'),len(dataList)) + print_end)

    @cherrypy.expose
    def delBG(self,bgdh):#删变更记录要从后往前删
        sql_check = """
            SELECT MAX(BGDH) 
            FROM T_SBBG 
            WHERE SBBH IN (SELECT SBBH FROM T_SBBG WHERE BGDH = '%s')
        """
        if bgdh != ms.ExecQuery(sql_check % bgdh)[0][0]:
            return "no"
        else:
            sql = "DELETE FROM T_SBBG WHERE BGDH = '%s';" % bgdh
            ms.ExecNonQuery(sql)
            return "ok"